/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Fenetre.java
 *
 * Created on 3 mai 2012, 11:26:11
 */

package sporejava;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author antonin
 */
public class Fenetre extends javax.swing.JFrame {

    private Environnement env;
    private PanelEnv pe;

    public Fenetre() {
        initComponents();
    }

    public void startEnvironnement()
    {
        env = new Environnement();
        pe = new PanelEnv(this.size(), env.getWidth(), env.getHeight());
        this.add(pe);
        this.pack();
        env.init();
        while(!env.estVide()){
            env.printEnv();
            env.cycleSuivant();
            pe.repaint();
        }
    }

    public class PanelEnv extends JPanel{
        private int wi;
        private int he;

        public PanelEnv(Dimension d, int w, int h){
            wi=w;
            he=h;
            this.setSize(d);
            this.setBackground(Color.white);
            this.setVisible(true);
            repaint();
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            int x = this.getWidth() / he;
            int y = this.getHeight() / wi;
            for(int i = 1 ; i<=wi ; i++){
                g.drawLine(0, y * i, this.getWidth(), y * i);
            }
            for(int i = 0 ; i<=he ; i++){
                g.drawLine(x * i, 0, x * i, this.getHeight());
            }
            if(env.getEv().size()>0){
                for(EtreVivant ev : env.getEv()){
                    if(ev instanceof Vegetal){
                        g.setColor(Color.GREEN);
                        g.fillOval((int)(ev.getPosition()[0]*x + Math.random()*x), (int)(ev.getPosition()[1]*y + Math.random()*y), 20, 20);
                    }
                    if(ev instanceof Carnivore){
                        g.setColor(Color.RED);
                        g.fillRect((int)(ev.getPosition()[0]*x + Math.random()*x), (int)(ev.getPosition()[1]*y + Math.random()*y), 20, 20);
                    }
                    if(ev instanceof Herbivore){
                        g.setColor(Color.YELLOW);
                        g.fillRect((int)(ev.getPosition()[0]*x + Math.random()*x), (int)(ev.getPosition()[1]*y + Math.random()*y), 20, 20);
                    }
                }
            }
        }

        public void placeEv(ArrayList<EtreVivant> ev){

        }
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
